using UnityEditor;
using UnityEngine;

public class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObject
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                var findAssets = AssetDatabase.FindAssets($"t:{typeof(T).Name}");
                if (findAssets == null || findAssets.Length == 0)
                {
                    Debug.LogError($"No assets of type {typeof(T).Name}");
                }
                else if (findAssets.Length > 1)
                {
                    Debug.LogError($"Multi assets of type {typeof(T).Name}");
                }
                else
                {
                    instance = AssetDatabase.LoadAssetAtPath<T>(AssetDatabase.GUIDToAssetPath(findAssets[0]));
                }
            }
            return instance;
        }
    }
}